﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Librerias;

namespace Aplicacion.procesos
{
    public partial class siga : System.Web.UI.Page
    {
        string line;

        ObjetoAd _ad = new ObjetoAd();
        ObjetoAd _nuevoUser = new ObjetoAd();

        protected void Page_Load(object sender, EventArgs e)
        {

            // create a writer and open the file

            TextWriter tw = new StreamWriter(@"c:\ad\usuariosyaexistentes.txt");


            StreamReader file = null;
            // create reader & open file
            file = new StreamReader(@"c:\ad\nuevosad.txt");

            int i = 0;

            while ((line = file.ReadLine()) != null)
            {

                string[] datos = line.Split(new string[] { "::::" }, StringSplitOptions.RemoveEmptyEntries);
                if (datos.Length > 0)
                {
                    string sAMAccountName = datos[0].Trim();
                    string displayName = datos[1].Trim();

                    //Response.Write(sAMAccountName + " - " + displayName.ToUpper());
                    //Response.Write("CN=" + displayName.ToUpper().Trim() + "," + Configuracion.Get("ad_ou_users"));
                    string[] tmp = displayName.Split(' ');
                    string apellido = tmp[0];
                    string nombre = tmp[1];
                    if (tmp.Length > 2)
                    {
                        nombre = nombre + " " + tmp[2];
                    }


                    string password = "Tdf" + i + "siga";

                    var busqueda = _ad.Buscar(sAMAccountName);
                    if (busqueda != null && busqueda.Count >= 1)
                    {
                        SearchResultCollection resultado = busqueda;
                        ObjetoAd usuario = new ObjetoAd(resultado[0].Path);
                        usuario.UserPassword = "Tdf" + i + "siga";

                        // write a line of text to the file
                        //tw.WriteLine(sAMAccountName + " :::: " + displayName + " ya existe");
                        Response.Write(sAMAccountName + " --- Tdf" + i + "siga <br />");
                    }
                    else
                    {
                        Response.Write(sAMAccountName + " *** NO existe <br />");
                    /*   
                    _nuevoUser = _ad.NuevoHijo("CN=" + displayName.ToUpper().Trim() + "," + Configuracion.Get("ad_ou_users"), "user");
                    _nuevoUser.SamAccountName = _nuevoUser.UserPrincipalName = _nuevoUser.Mail = sAMAccountName;
                    _nuevoUser.Uid = sAMAccountName;
                    _nuevoUser.MsSfu30Name = sAMAccountName;
                    _nuevoUser.DisplayName = displayName;
                    _nuevoUser.Nombre = nombre;
                    _nuevoUser.Apellido = apellido;
                    _nuevoUser.UserPassword = password;
                    _nuevoUser.UserFlags = ObjetoAd.NormalAccount + ObjetoAd.DontExpirePassword;
                    _nuevoUser.NisDomain = "tierradelfuego";
                    _nuevoUser.HomeDirectory = "/homes/" + sAMAccountName;

                    if (_nuevoUser.UidNumber == 0)
                    {
                        _nuevoUser.SetUltimoUid();
                    }
                    _nuevoUser.GidNumber = 10007;
                    _nuevoUser.Guardar();
                    */

                        Response.Write(line + " :::: Tdf" + i + "siga" + "<br/>");
                    }



                }

                //if (i == 2)
                //    break;
                i++;

            }
            // close the stream
            file.Close();
            // close the stream
            tw.Close();
        }
    }
}